﻿#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfFootball.Parser;
#endregion

namespace WpfFootball.UI
{
    /// <summary>
    /// Interaction logic for NextMatchViewer.xaml
    /// </summary>
    public partial class NextMatchViewer: UpdatableView
    {
        #region Fields
        private DataParser parser;
        private string teamName;
        private NextGameEntry nextGameEntry;
        #endregion

        public NextMatchViewer(string teamName)
        {
            this.teamName = teamName;
            InitializeView();
            RegisterEventHandlers();
        }

        public override void RegisterEventHandlers()
        {
            base.RegisterEventHandlers();
            parser.ParseNextTeamGameCompleted += NextTeamGameParsed;
        }

        public override void InitializeView()
        {
            parser = new DataParser();
            InitializeComponent();
            SetViewLocation();
            BeginUpdate();
            ScaleUpViewAnimation();
        }

        public override void SetViewLocation()
        {
            Canvas.SetLeft(this, 420);
            Canvas.SetTop(this, 10);    
        }

        public override void CommitViewLocation()
        { }

        public void NextTeamGameParsed(object sender, NextGameEntry arg)
        {
            nextGameEntry = arg;
            Update();
        }

        public void BeginUpdate()
        {
            parser.BeginParseTeamNextGame(teamName);
        }

        /// <summary>
        /// Updates the information about the next game of the current team.
        /// Called when the parsing process is done
        /// </summary>
        public void Update()
        {
            stackPanel1.Dispatcher.BeginInvoke(
                new Action(() =>
                {
                    TeamA = nextGameEntry.teamA;
                    TeamB = nextGameEntry.teamB;
                    TimeStamp = nextGameEntry.timeStamp;
                }));
        }

        /// <summary>
        /// Gets and sets the name of the home team.
        /// </summary>
        public string TeamA
        {
            get
            {
                return team1TextBlock.Text;
            }
            set
            {
                team1TextBlock.Text = value;
            }
        }

        /// <summary>
        /// Gets and sets the name of the visiting team.
        /// </summary>
        public string TeamB
        {
            get
            {
                return team2TextBlock.Text;
            }
            set
            {
                team2TextBlock.Text = value;
            }
        }

        /// <summary>
        /// Gets and sets the timestamp for the next game.
        /// </summary>
        public string TimeStamp
        {
            get
            {
                return timeStampBlock.Text;
            }
            set
            {
                timeStampBlock.Text = value;
            }
        }
                
    }
}
